<!DOCTYPE html>
<html lang="en">
<head>
  <title>Router - Born to build better enterprise frameworks and apps</title>
  <meta charset="utf-8">
  <meta name="description" content="index.description">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <link rel="icon" href="/images/favicon.png" type="image/x-icon">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css" />
<link rel="stylesheet" href="/css/index.css">

    <script>
    !function(t,e,a,r,c){t.TracertCmdCache=t.TracertCmdCache||[],t[c]=window[c]||
      {_isInit:!0,call:function(){t.TracertCmdCache.push(arguments)},
      start:function(t){this.call('start',t)}},t[c].l=new Date;
      var n=e.createElement(a),s=e.getElementsByTagName(a)[0];
      n.async=!0,n.src=r,s.parentNode.insertBefore(n,s)}
    (window,document,'script','https://tracert.alipay.com/tracert.js','Tracert');
      Tracert.start({
        plugins: [ 'BucName' ],
        spmAPos: 'a454',
        spmBPos: 'b4893',
      });
    </script>
  
</head>
<body>
  <div class="nav" >
  <header>
    <a href="/en/" class="nav-logo leftpadding" alt="egg"><img src="https://zos.alipayobjects.com/rmsportal/VTcUYAaoKqXyHJbLAPyF.svg"></a>
    <ul class="nav-item">
      <li>
        <form id="search-form">
          <input type="text" id="search-query" class="search-query st-default-search-input">
        </form>
      </li>
      <li><a href="/en/intro/" alt="Guide">Guide</a></li><li><a href="/api/" alt="API">API</a></li><li><a href="/en/tutorials/index.html" alt="Tutorials">Tutorials</a></li><li><a href="https://github.com/search?q=topic%3Aegg-plugin&type=Repositories" alt="Plugins">Plugins</a></li><li><a href="https://github.com/eggjs/egg/releases" alt="Release">Release</a></li>
      
      
        <li class="translations">
          <a class="nav-link">中文文档</a>
          <span class="arrow"></span><ul id="dropdownContent" class="dropdown-content"><li><a id="en" href="/en/basics/router.html" style="color: #22ab28">English</a></li><li><a id="zh-cn" href="/zh-cn/basics/router.html" >中文</a></li></ul>
        </li>
      
      <li><iframe src="https://ghbtns.com/github-btn.html?user=eggjs&repo=egg&type=star&count=true" frameborder="0" scrolling="0" width="150px" height="20px"></iframe></li>
    </ul>
    <a id="mobileTrigger" href="#" class="mobile-trigger">
      <ul>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </a>
  </header>
</div>
  <div id="container" class="container">
    <div class="page-main">
  <article class="markdown-body">
    <h1>Router</h1>
    <p>Router is mainly used to describe the corresponding relationship between the request URL and the Controller that processes the request eventually. All routing rules are unified in the <code>app/router.js</code> file by the framework.</p>
<p>By unifying routing rules, we can avoid the routing logics scattered in many places which may cause many unknown conflicts, and we can more easily check global routing rules.</p>
<h2 id="how-to-define-router"><a class="markdown-anchor" href="#how-to-define-router">#</a> How to Define Router</h2>
<ul>
<li>Define the routing rule in <code>app/router.js</code> file</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; router, controller &#125; = app;</span><br><span class="line">  router.get(<span class="string">'/user/:id'</span>, controller.user.info);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li>Implement the Controller in <code>app/controller</code> directory</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/controller/user.js</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line">  <span class="keyword">async</span> info() &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="keyword">this</span>;</span><br><span class="line">    ctx.body = &#123;</span><br><span class="line">      name: <span class="string">`hello <span class="subst">$&#123;ctx.params.id&#125;</span>`</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>This simplest Router is done by now, when users do the request <code>GET /user/123</code>, the info function in <code>user.js</code> will be invoked.</p>
<h2 id="router-config-in-detail"><a class="markdown-anchor" href="#router-config-in-detail">#</a> Router config in detail</h2>
<p>Below is the complete definition of router, parameters can be determined depending on different scenes.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">router.verb(<span class="string">'path-match'</span>, app.controller.action);</span><br><span class="line">router.verb(<span class="string">'router-name'</span>, <span class="string">'path-match'</span>, app.controller.action);</span><br><span class="line">router.verb(<span class="string">'path-match'</span>, middleware1, ..., middlewareN, app.controller.action);</span><br><span class="line">router.verb(<span class="string">'router-name'</span>, <span class="string">'path-match'</span>, middleware1, ..., middlewareN, app.controller.action);</span><br></pre></td></tr></table></figure>
<p>The complete definition of router includes 5 major parts:</p>
<ul>
<li>verb - actions that users trigger, including get, post and so on, and will be explained in detail later.
<ul>
<li>router.head - HEAD</li>
<li>router.options - OPTIONS</li>
<li>router.get - GET</li>
<li>router.put - PUT</li>
<li>router.post - POST</li>
<li>router.patch - PATCH</li>
<li>router.delete - DELETE</li>
<li>router.del - this is a alias method due to the reservation of delete.</li>
<li>router.redirect - redirects the request URL. For example, the most common case is to redirect the request accessing the root directory to the homepage.</li>
</ul>
</li>
<li>router-name defines a alias for the route, and URL can be generated by helper method <code>pathFor</code> and <code>urlFor</code> provided by Helper. (Optional)</li>
<li>path-match - URL path of the route.</li>
<li>middleware1 - multiple Middlewares can be configured in Router. (Optional)</li>
<li>controller - set the route to map to the specific controller, and the controller can be written in two types:
<ul>
<li><code>app.controller.user.fetch</code> - directly point to a controller</li>
<li><code>'user.fetch'</code> - simplified as a string,</li>
</ul>
</li>
</ul>
<h3 id="notices"><a class="markdown-anchor" href="#notices">#</a> Notices</h3>
<ul>
<li>multiple Middlewares can be configured to execute serially in Router definition</li>
<li>Controller must be defined under <code>app/controller</code> directory</li>
<li>multiple Controllers can be defined within one file, and the specific one can be specified in the form of <code>${fileName}.${functionName}</code> when defining the routing rule.</li>
<li>Controller supports sub-directories, and the specific one can be specified in the form of <code>${directoryName}.${fileName}.${functionName}</code> when defining the routing rule.</li>
</ul>
<p>Here are some examples of writing routing rules:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; router, controller &#125; = app;</span><br><span class="line">  router.get(<span class="string">'/home'</span>, controller.home);</span><br><span class="line">  router.get(<span class="string">'/user/:id'</span>, controller.user.page);</span><br><span class="line">  router.post(<span class="string">'/admin'</span>, isAdmin, controller.admin);</span><br><span class="line">  router.post(<span class="string">'/user'</span>, isLoginUser, hasAdminPermission, controller.user.create);</span><br><span class="line">  router.post(<span class="string">'/api/v1/comments'</span>, controller.v1.comments.create); <span class="comment">// app/controller/v1/comments.js</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="restful-style-url-definition"><a class="markdown-anchor" href="#restful-style-url-definition">#</a> RESTful style URL definition</h3>
<p>We provide <code>app.resources('routerName', 'pathMatch', 'controller')</code> to generate <a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank" rel="noopener">CRUD</a> structures on a path for convenience if you prefer the RESTful style URL definition.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; router, controller &#125; = app;</span><br><span class="line">  router.resources(<span class="string">'posts'</span>, <span class="string">'/posts'</span>, controller.posts);</span><br><span class="line">  router.resources(<span class="string">'users'</span>, <span class="string">'/api/v1/users'</span>, controller.v1.users); <span class="comment">// app/controller/v1/users.js</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>The codes above produce a bunch of CRUD path structures for Controller <code>app/controller/posts.js</code>, and the only thing you should do next is to implement related functions in <code>posts.js</code>.</p>
<table>
<thead>
<tr>
<th>Method</th>
<th>Path</th>
<th>Route Name</th>
<th>Controller.Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/posts</td>
<td>posts</td>
<td>app.controllers.posts.index</td>
</tr>
<tr>
<td>GET</td>
<td>/posts/new</td>
<td>new_post</td>
<td>app.controllers.posts.new</td>
</tr>
<tr>
<td>GET</td>
<td>/posts/:id</td>
<td>post</td>
<td>app.controllers.posts.show</td>
</tr>
<tr>
<td>GET</td>
<td>/posts/:id/edit</td>
<td>edit_post</td>
<td>app.controllers.posts.edit</td>
</tr>
<tr>
<td>POST</td>
<td>/posts</td>
<td>posts</td>
<td>app.controllers.posts.create</td>
</tr>
<tr>
<td>PATCH</td>
<td>/posts/:id</td>
<td>post</td>
<td>app.controllers.posts.update</td>
</tr>
<tr>
<td>DELETE</td>
<td>/posts/:id</td>
<td>post</td>
<td>app.controllers.posts.destroy</td>
</tr>
</tbody>
</table>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/controller/posts.js</span></span><br><span class="line">exports.index = <span class="keyword">async</span> () =&gt; &#123;&#125;;</span><br><span class="line"></span><br><span class="line">exports.new = <span class="keyword">async</span> () =&gt; &#123;&#125;;</span><br><span class="line"></span><br><span class="line">exports.create = <span class="keyword">async</span> () =&gt; &#123;&#125;;</span><br><span class="line"></span><br><span class="line">exports.show = <span class="keyword">async</span> () =&gt; &#123;&#125;;</span><br><span class="line"></span><br><span class="line">exports.edit = <span class="keyword">async</span> () =&gt; &#123;&#125;;</span><br><span class="line"></span><br><span class="line">exports.update = <span class="keyword">async</span> () =&gt; &#123;&#125;;</span><br><span class="line"></span><br><span class="line">exports.destroy = <span class="keyword">async</span> () =&gt; &#123;&#125;;</span><br></pre></td></tr></table></figure>
<p>Methods that are not needed may not be implemented in <code>posts.js</code> and the related URL paths will not be registered to Router neither.</p>
<h2 id="router-in-action"><a class="markdown-anchor" href="#router-in-action">#</a> Router in Action</h2>
<p>More practical examples will be shown below to demonstrate how to use the router.</p>
<h4 id="acquiring-parameters"><a class="markdown-anchor" href="#acquiring-parameters">#</a> Acquiring Parameters</h4>
<h4 id="via-query-string"><a class="markdown-anchor" href="#via-query-string">#</a> via Query String</h4>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.get(<span class="string">'/search'</span>, app.controller.search.index);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/controller/search.js</span></span><br><span class="line">exports.index = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">  ctx.body = <span class="string">`search: <span class="subst">$&#123;ctx.query.name&#125;</span>`</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// curl http://127.0.0.1:7001/search?name=egg</span></span><br></pre></td></tr></table></figure>
<h4 id="via-named-parameters"><a class="markdown-anchor" href="#via-named-parameters">#</a> via Named Parameters</h4>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.get(<span class="string">'/user/:id/:name'</span>, app.controller.user.info);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/controller/user.js</span></span><br><span class="line">exports.info = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">  ctx.body = <span class="string">`user: <span class="subst">$&#123;ctx.params.id&#125;</span>, <span class="subst">$&#123;ctx.params.name&#125;</span>`</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// curl http://127.0.0.1:7001/user/123/xiaoming</span></span><br></pre></td></tr></table></figure>
<h4 id="acquiring-complex-parameters"><a class="markdown-anchor" href="#acquiring-complex-parameters">#</a> acquiring complex parameters</h4>
<p>Regular expressions, as well, can be used in routing rules to acquire parameters more flexibly:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.get(<span class="regexp">/^\/package\/([\w-.]+\/[\w-.]+)$/</span>, app.controller.package.detail);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/controller/package.js</span></span><br><span class="line">exports.detail = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">  <span class="comment">// If the request URL is matched by the regular expression, parameters can be acquired from ctx.params according to the capture group orders.</span></span><br><span class="line">  <span class="comment">// For the user request below, for example, the value of `ctx.params[0]` is `egg/1.0.0`</span></span><br><span class="line">  ctx.body = <span class="string">`package:<span class="subst">$&#123;ctx.params[<span class="number">0</span>]&#125;</span>`</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// curl http://127.0.0.1:7001/package/egg/1.0.0</span></span><br></pre></td></tr></table></figure>
<h3 id="acquiring-form-contents"><a class="markdown-anchor" href="#acquiring-form-contents">#</a> Acquiring Form Contents</h3>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.post(<span class="string">'/form'</span>, app.controller.form.post);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/controller/form.js</span></span><br><span class="line">exports.post = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">  ctx.body = <span class="string">`body: <span class="subst">$&#123;<span class="built_in">JSON</span>.stringify(ctx.request.body)&#125;</span>`</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// simulate a post request.</span></span><br><span class="line"><span class="comment">// curl -X POST http://127.0.0.1:7001/form --data '&#123;"name":"controller"&#125;' --header 'Content-Type:application/json'</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>P.S.:</p>
</blockquote>
<blockquote>
<p>If you perform a POST request directly, an <strong>error</strong> will occur: 'secret is missing'. This error message comes from <a href="https://github.com/koajs/csrf/blob/2.5.0/index.js#L69" target="_blank" rel="noopener">koa-csrf/index.js#L69</a>.</p>
</blockquote>
<blockquote>
<p><strong>Reason</strong>: the framework verifies the CSFR value specially for form POST requests, so please submit the CSRF key as well when you submit a form. Refer to <a href="https://eggjs.org/zh-cn/core/security.html#%E5%AE%89%E5%85%A8%E5%A8%81%E8%83%81csrf%E7%9A%84%E9%98%B2%E8%8C%83">Keep Away from CSRF Threat</a> for more detail.</p>
</blockquote>
<blockquote>
<p><strong>Note</strong>: the verification is performed because the framework builds in a security plugin <a href="https://github.com/eggjs/egg-security" target="_blank" rel="noopener">egg-security</a> that provides some default security practices and this plugin is enabled by default. In case you want to disable some security protections, just set the enable attribute to false.</p>
</blockquote>
<blockquote>
<p>&quot;Unless you clearly confirm the consequence, it's not recommended to disable functions provided by the security plugin&quot;</p>
</blockquote>
<blockquote>
<p>Here we do the config temporarily in <code>config/config.default.js</code> for an example</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">exports.security = &#123;</span><br><span class="line">  csrf: false</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="form-verification"><a class="markdown-anchor" href="#form-verification">#</a> Form Verification</h3>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.post(<span class="string">'/user'</span>, app.controller.user);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/controller/user.js</span></span><br><span class="line"><span class="keyword">const</span> createRule = &#123;</span><br><span class="line">  username: &#123;</span><br><span class="line">    type: <span class="string">'email'</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  password: &#123;</span><br><span class="line">    type: <span class="string">'password'</span>,</span><br><span class="line">    compare: <span class="string">'re-password'</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">exports.create = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">  <span class="comment">// throws exceptions if the verification fails</span></span><br><span class="line">  ctx.validate(createRule);</span><br><span class="line">  ctx.body = ctx.request.body;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// curl -X POST http://127.0.0.1:7001/user --data 'username=abc@abc.com&amp;password=111111&amp;re-password=111111'</span></span><br></pre></td></tr></table></figure>
<h3 id="redirection"><a class="markdown-anchor" href="#redirection">#</a> Redirection</h3>
<h4 id="internal-redirection"><a class="markdown-anchor" href="#internal-redirection">#</a> Internal Redirection</h4>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.get(<span class="string">'index'</span>, <span class="string">'/home/index'</span>, app.controller.home.index);</span><br><span class="line">  app.redirect(<span class="string">'/'</span>, <span class="string">'/home/index'</span>, <span class="number">302</span>);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/controller/home.js</span></span><br><span class="line">exports.index = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">  ctx.body = <span class="string">'hello controller'</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// curl -L http://localhost:7001</span></span><br></pre></td></tr></table></figure>
<h4 id="external-redirection"><a class="markdown-anchor" href="#external-redirection">#</a> External Redirection</h4>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.get(<span class="string">'/search'</span>, app.controller.search.index);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/controller/search.js</span></span><br><span class="line">exports.index = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> type = ctx.query.type;</span><br><span class="line">  <span class="keyword">const</span> q = ctx.query.q || <span class="string">'nodejs'</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (type === <span class="string">'bing'</span>) &#123;</span><br><span class="line">    ctx.redirect(<span class="string">`http://cn.bing.com/search?q=<span class="subst">$&#123;q&#125;</span>`</span>);</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    ctx.redirect(<span class="string">`https://www.google.co.kr/search?q=<span class="subst">$&#123;q&#125;</span>`</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// curl http://localhost:7001/search?type=bing&amp;q=node.js</span></span><br><span class="line"><span class="comment">// curl http://localhost:7001/search?q=node.js</span></span><br></pre></td></tr></table></figure>
<h3 id="using-middleware"><a class="markdown-anchor" href="#using-middleware">#</a> Using Middleware</h3>
<p>A middleware can be used to change the request parameter to uppercase.
Here we just briefly explain how to use the middleware, and refer to <a href="./middleware.html">Middleware</a> for detail.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/controller/search.js</span></span><br><span class="line">exports.index = <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">  ctx.body = <span class="string">`search: <span class="subst">$&#123;ctx.query.name&#125;</span>`</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/middleware/uppercase.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">uppercase</span>(<span class="params">ctx, next</span>) </span>&#123;</span><br><span class="line">    ctx.query.name = ctx.query.name &amp;&amp; ctx.query.name.toUpperCase();</span><br><span class="line">    <span class="keyword">await</span> next();</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.get(<span class="string">'s'</span>, <span class="string">'/search'</span>, app.middlewares.uppercase(), app.controller.search)</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// curl http://localhost:7001/search?name=egg</span></span><br></pre></td></tr></table></figure>
<h3 id="too-many-routing-maps"><a class="markdown-anchor" href="#too-many-routing-maps">#</a> Too Many Routing Maps?</h3>
<p>As described above, we do not recommend that you scatter routing logics all around, or it will bring trouble in trouble shooting.</p>
<p>If there is a need for some reasons, you can split routing rules like below:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">require</span>(<span class="string">'./router/news'</span>)(app);</span><br><span class="line">  <span class="built_in">require</span>(<span class="string">'./router/admin'</span>)(app);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/router/news.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.get(<span class="string">'/news/list'</span>, app.controller.news.list);</span><br><span class="line">  app.router.get(<span class="string">'/news/detail'</span>, app.controller.news.detail);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/router/admin.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.router.get(<span class="string">'/admin/user'</span>, app.controller.admin.user);</span><br><span class="line">  app.router.get(<span class="string">'/admin/log'</span>, app.controller.admin.log);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>or using <a href="https://github.com/eggjs/egg-router-plus" target="_blank" rel="noopener">egg-router-plus</a>.</p>

  </article>
  <aside id="mobileAside" class="toc">
  <div class="mobile-menu">
    <ul>
      <li><a href="/en/intro/" alt="Guide">Guide</a></li><li><a href="/api/" alt="API">API</a></li><li><a href="/en/tutorials/index.html" alt="Tutorials">Tutorials</a></li><li><a href="https://github.com/search?q=topic%3Aegg-plugin&type=Repositories" alt="Plugins">Plugins</a></li><li><a href="https://github.com/eggjs/egg/releases" alt="Release">Release</a></li>
      
      
        <li class="translations">
          <a class="nav-link">中文文档</a>
          <span class="arrow"></span><ul id="dropdownContent" class="dropdown-content"><li><a id="en" href="/en/basics/router.html" style="color: #22ab28">English</a></li><li><a id="zh-cn" href="/zh-cn/basics/router.html" >中文</a></li></ul>
        </li>
      
    </ul>
  </div>
  <dl><dt id="title-Intro" style="cursor: pointer;" class="aside-title">Guide<a id="collapse-icon-Intro" class="icon opend"></a></dt><dd id=panel-Intro><ul><li><a href="/en/intro/index.html" class="menu-link">What is Egg?</a></li><li><a href="/en/intro/egg-and-koa.html" class="menu-link">Egg and Koa</a></li><li><a href="/en/intro/quickstart.html" class="menu-link">Quick Start</a></li><li><a href="/en/tutorials/progressive.html" class="menu-link">Progressive</a></li><li><a href="/en/migration.html" class="menu-link">Migration to 2.x</a></li></ul></dd><dt id="title-Basics" style="cursor: pointer;" class="aside-title">Basis Function<a id="collapse-icon-Basics" class="icon opend"></a></dt><dd id=panel-Basics><ul><li><a href="/en/basics/structure.html" class="menu-link">Directory Structure</a></li><li><a href="/en/basics/objects.html" class="menu-link">Built-in Objects</a></li><li><a href="/en/basics/env.html" class="menu-link">Environment</a></li><li><a href="/en/basics/config.html" class="menu-link">Configuration</a></li><li><a href="/en/basics/middleware.html" class="menu-link">Middleware</a></li><li><a href="/en/basics/router.html" class="menu-link">Router</a></li><li><a href="/en/basics/controller.html" class="menu-link">Controller</a></li><li><a href="/en/basics/service.html" class="menu-link">Service</a></li><li><a href="/en/basics/plugin.html" class="menu-link">Plugin</a></li><li><a href="/en/basics/schedule.html" class="menu-link">Schedule</a></li><li><a href="/en/basics/extend.html" class="menu-link">Extend</a></li><li><a href="/en/basics/app-start.html" class="menu-link">Custom Init</a></li></ul></dd><dt id="title-Core" style="cursor: pointer;" class="aside-title">Core<a id="collapse-icon-Core" class="icon opend"></a></dt><dd id=panel-Core><ul><li><a href="/en/core/development.html" class="menu-link">Development</a></li><li><a href="/en/core/unittest.html" class="menu-link">Unit Testing</a></li><li><a href="/en/core/deployment.html" class="menu-link">Deployment</a></li><li><a href="/en/core/logger.html" class="menu-link">Logger</a></li><li><a href="/en/core/httpclient.html" class="menu-link">HttpClient</a></li><li><a href="/en/core/cookie-and-session.html" class="menu-link">Cookie and Session</a></li><li><a href="/en/core/cluster-and-ipc.html" class="menu-link">Cluster and IPC</a></li><li><a href="/en/core/view.html" class="menu-link">View</a></li><li><a href="/en/core/error-handling.html" class="menu-link">Error Handling</a></li><li><a href="/en/core/security.html" class="menu-link">Security</a></li><li><a href="/en/core/i18n.html" class="menu-link">i18n</a></li></ul></dd><dt id="title-Tutorials" style="cursor: pointer;" class="aside-title">Tutorials<a id="collapse-icon-Tutorials" class="icon opend"></a></dt><dd id=panel-Tutorials><ul><li><a href="/en/tutorials/mysql.html" class="menu-link">MySQL</a></li><li><a href="/en/tutorials/restful.html" class="menu-link">RESTful API</a></li><li><a href="/en/tutorials/passport.html" class="menu-link">Passport</a></li><li><a href="/en/tutorials/socketio.html" class="menu-link">Socket.IO</a></li><li><a href="/en/tutorials/assets.html" class="menu-link">Assets</a></li><li><a href="/en/tutorials/typescript.html" class="menu-link">TypeScript</a></li></ul></dd><dt id="title-Advanced" style="cursor: pointer;" class="aside-title">Advanced<a id="collapse-icon-Advanced" class="icon opend"></a></dt><dd id=panel-Advanced><ul><li><a href="/en/advanced/loader.html" class="menu-link">Loader</a></li><li><a href="/en/advanced/plugin.html" class="menu-link">Plugin Development</a></li><li><a href="/en/advanced/framework.html" class="menu-link">Framework</a></li><li><a href="/en/advanced/cluster-client.html" class="menu-link">Cluster Enhancement</a></li><li><a href="/en/advanced/view-plugin.html" class="menu-link">View Plugin</a></li><li><a href="/en/style-guide.html" class="menu-link">Style Guide</a></li></ul></dd><dt id="title-Community" style="cursor: pointer;" class="aside-title">Community<a id="collapse-icon-Community" class="icon opend"></a></dt><dd id=panel-Community><ul><li><a href="/en/plugins/" class="menu-link">Plugin List</a></li><li><a href="/en/contributing.html" class="menu-link">Contributing</a></li><li><a href="/en/resource.html" class="menu-link">Resource</a></li><li><a href="/en/faq.html" class="menu-link">FAQ</a></li></ul></dd></dl>
</aside>
<script>
var mobileTrigger = document.getElementById('mobileTrigger');
var mobileAside = document.getElementById('mobileAside');

var expandMenu = function(title) {
  // handle icon
  const collapseIcon = document.getElementById('collapse-icon-' + title);
  if (collapseIcon) {
    collapseIcon.className = 'icon opend';
  }
  // handle panelEle
  const panelEle = document.getElementById('panel-' + title);
  if (panelEle) {
    panelEle.className = '';
  }
}

var collapseMenu = function(title) {
  // handle icon
  const collapseIcon = document.getElementById('collapse-icon-' + title);
  if (collapseIcon) {
    collapseIcon.className = 'icon closed';
  }
  // handle panelEle
  const panelEle = document.getElementById('panel-' + title);
  if (panelEle) {
    panelEle.className = 'aside-panel-hidden';
  }
}

mobileAside.onclick = function(e) {
  const targetId = e.target.id;
  if (targetId && (targetId.indexOf('title-') > -1 || targetId.indexOf('collapse-icon-') > -1)) {
    const title = targetId.replace('title-', '').replace('collapse-icon-', '');
    try { 
      // the the browser may have no localStroage or JSON.parse may throw exception.
      const menuInfo = JSON.parse(window.localStorage.getItem('menuInfo'));
        
      // current menu status
      const curClosed = menuInfo[title] ? menuInfo[title].closed : false; // default false

      // change UI
      curClosed ? expandMenu(title) : collapseMenu(title);

      // save menuInfo to localStorage
      menuInfo[title] = { closed: !curClosed } // opposite
      window.localStorage.setItem('menuInfo', JSON.stringify(menuInfo));
    } catch (e) {}
  }
};

mobileTrigger.onclick = function(e) {
  e.preventDefault();
  if (mobileAside.className.indexOf('mobile-show') === -1) {
    mobileAside.className += ' mobile-show';
  } else {
    mobileAside.className = 'toc';
  }
};

(function() {
  // save data to localStorage because the page will refresh when user change the url.
  let menuInfo;
  try { 
    // the the browser may have no localStroage or JSON.parse may throw exception.
    menuInfo = JSON.parse(window.localStorage.getItem('menuInfo'));
    if (!menuInfo) {
      menuInfo = {};
      window.localStorage.setItem('menuInfo', JSON.stringify(menuInfo));
    }
  } catch (e) {
    menuInfo = {}; // default {}
  }

  for (const title in menuInfo) {
    if (menuInfo[title] && menuInfo[title].closed) { // menu in closed status.
      collapseMenu(title);
    } else {
      expandMenu(title);
    }
  }

  // highlight menu
  const pathname = window.location.pathname;
  const selector = `a[href="${pathname}"].menu-link,a[href="${pathname}index.html"].menu-link`;
  const menuItem = mobileAside.querySelector(selector);
  if (menuItem) { menuItem.className += ' highlight'; }
})();
</script>

</div>

  </div>
</body>
<script src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script>
<script>
docsearch({
  apiKey: '1561de31a86f79507ea00cdb54ce647c',
  indexName: 'eggjs',
  inputSelector: '#search-query',
});
</script>
<div class="cnzz">
<script src="https://s11.cnzz.com/z_stat.php?id=1261142226&web_id=1261142226" language="JavaScript"></script>
</div>

</html>
